package com.zzy.controller;

import com.zzy.pojo.Account;
import com.zzy.pojo.MyRest;
import com.zzy.pojo.Order;
import com.zzy.pojo.OrderMsg;
import com.zzy.service.serviceimpl.CzServiceImpl;
import com.zzy.utlis.OrderId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mall")
public class MyMallRest
{
    @Resource
    CzServiceImpl czService;
    @Resource
    RedisTemplate<String,Object> redisTemplate;
    //查询账户及其余额
    @RequestMapping("/czzh")
    public MyRest czzh(@RequestBody Map<String,String> map )
    {
        MyRest myRest = new MyRest();
        String accountID = map.get("accountID");
        Account account = czService.getAccount(accountID);

        //生成一个随机字符串
        String orderIdByTime = OrderId.getOrderIdByTime();
        redisTemplate.opsForValue().set("zzy"+orderIdByTime,1);//存到reids

        if(account==null)
        {
            myRest.setCode(100);
            myRest.setMsg("no");
            return myRest;
        }
        else
        {
            Map<String,Object> m = new HashMap<>();
            m.put("account",account);
            m.put("orderId",orderIdByTime);
            myRest.setCode(200);
            myRest.setMsg("ok");
            myRest.setData(m);
            return myRest;
        }

    }
    //支付宝充值提交订单
    @RequestMapping("/paycz.do")
    public MyRest paycz(@RequestBody Order order)
    {
        MyRest myRest = new MyRest();
        //判断是不是第一次进接口
        if(redisTemplate.hasKey("zzy"+order.getOrderId()))
        {
            //是第一次，删除掉这个key,执行操作
            redisTemplate.delete("zzy"+order.getOrderId());
            OrderMsg orderMsg = new OrderMsg();
            orderMsg.setOut_trade_no("zzy"+order.getOrderId());
            orderMsg.setSubject(order.getOrderId());
            orderMsg.setNotify_url("http://538s69k093.qicp.vip:11058/mall/payresult.do");
            orderMsg.setReturn_url("http://538s69k093.qicp.vip:11058/zfcg.html");
            BigDecimal orderMoney = order.getOrderMoney();
            String money = String.valueOf(orderMoney);
            orderMsg.setTotal_amount(money);
            //调用支付宝支付接口
            String url="http://127.0.0.1:8189/wangguan/zhifu";
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<MyRest> entity = restTemplate.postForEntity(url,orderMsg,MyRest.class);
            MyRest body = entity.getBody();
            Object data = body.getData();
            myRest.setMsg("ok");
            myRest.setData( body.getData());
            myRest.setCode(200);
            return myRest;
        }
        else
        {
            myRest.setMsg("no");
            myRest.setCode(201);
            return myRest;
        }
    }
}
